﻿// ***********************************************************************
// Provided for Informational Purposes Only
//
// Apache 2.0 License
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY 
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR 
// PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
// ***********************************************************************
// Assembly  : ContractMeow.Api.WinRT
// File      : AutoCompletionClient.cs
//
// ***********************************************************************
using ContractMeow.Api.Dtos;
using ContractMeow.Api.Dtos.AutoComplete;
using ContractMeow.WinRT.Serialization;
using System.Net;
using System.Threading.Tasks;

namespace ContractMeow.Api.Clients
{
	/// <summary>
	/// Class AutoCompleteClient
	/// </summary>
	public sealed class AutoCompleteClient : ClientBase, IAutoCompleteClient
	{
		#region Resources
		/// <summary>
		/// The users resource
		/// </summary>
		private const string UsersResource = "api/v1/autocomplete/ranked";
		#endregion

		/// <summary>
		/// Initializes a new instance of the <see cref="AutoCompleteClient" /> class.
		/// </summary>
		/// <param name="configuration">The configuration.</param>
		/// <param name="queryStringSerializer">The query string serializer.</param>
		/// <param name="serializer">The serializer.</param>
		/// <param name="deserializer">The deserializer.</param>
		/// <param name="responseErrorHandler">The response error handler.</param>
		/// <param name="cookieStore">The cookie store.</param>
		/// <param name="proxy">The proxy.</param>
		public AutoCompleteClient(IClientConfiguration configuration,
								  IQueryStringSerializer queryStringSerializer,
								  ISerializer serializer,
								  IDeserializer deserializer,
								  IResponseErrorHandler responseErrorHandler,
								  ICookieStore cookieStore,
								  IWebProxy proxy = null)
			: base(configuration, queryStringSerializer, serializer, deserializer, responseErrorHandler, cookieStore, proxy) { }

		/// <summary>
		/// Gets the users [auto complete] list (results will be ranked according to user's preferences).
		/// </summary>
		/// <param name="credentials">The credentials.</param>
		/// <param name="prefix">The first few letters to be used for the search</param>
		/// <param name="limit">Maximum number of users to bring back</param>
		/// <param name="includePendingUsers">Include a list of pending users.</param>
		/// <returns>The users matching the prefix wrapped in an instance of IAutoCompleteResults.</returns>
		public async Task<AutoCompleteUserEnvelope> GetUsersAutocompleteAsync(Principal credentials,
																		 string prefix,
																		 int limit = 50,
																		 bool includePendingUsers = false)
		{
			VerifyCredentials(credentials);

			var parms = new
				{
					prefix,
					models = string.Format("user:{0}", limit),
					secondOrderRanks = "true",
					includePendingUsers
				};

			var r = await GetAsync<AutoCompleteUserEnvelope>(UsersResource, credentials, parms);

			return r.Result();
		}

		/// <summary>
		/// Gets the groups (auto complete) list for the credentials (results will be ranked according to user's preferences)
		/// </summary>
		/// <param name="credentials"></param>
		/// <param name="prefix">The first few letters to be used for the search</param>
		/// <param name="limit">Maximums number of groups to return</param>
		/// <returns>The group names matching the prefix wrapped in an instance of IAutoCompleteResults.</returns>
		public async Task<AutoCompleteGroupEnvelope> GetGroupsAutocompleteAsync(Principal credentials,
																		   string prefix,
																		   int limit = 50)
		{
			VerifyCredentials(credentials);

			var parms = new
				{
					prefix,
					models = string.Format("group:{0}", limit),
					secondOrderRanks = "true",
				};

			var r = await GetAsync<AutoCompleteGroupEnvelope>(UsersResource, credentials, parms);

			var result = r.Result();
			return result;
		}
	}
}